package com.mmdccj.yugu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.dto.SearchMusicDto;
import com.mmdccj.yugu.dto.UserDto;
import com.mmdccj.yugu.entity.User;
import com.mmdccj.yugu.mapper.FollowMapper;
import com.mmdccj.yugu.mapper.SearchMapper;
import com.mmdccj.yugu.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: star_
 * @date: 2023-12-09 16:26
 */
@Slf4j
@Service
public class SearchServiceImpl implements SearchService {
    // 自动注入SearchMapper实例
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private FollowMapper followMapper;

    /**
     * 根据关键字搜索音乐、歌手和歌单
     *
     * @param keyword     搜索关键字
     * @param currentPage 当前页码
     * @param pageSize    每页大小
     * @return SearchMusicDto 包含音乐、歌手和歌单信息的DTO
     */
    @Override
    public SearchMusicDto searchMusic(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        SearchMusicDto searchMusicDto = new SearchMusicDto();
        // 获取音乐信息
        searchMusicDto.setMusicList(new PageInfo<>(searchMapper.selectMusicByKeyword(keyword)));
        // 获取歌手信息
        searchMusicDto.setUserList(new PageInfo<>(searchMapper.selectSingerNoDistinct(keyword)));

        return searchMusicDto;
    }

    /**
     * 根据关键字搜索歌手
     *
     * @param keyword     搜索关键字
     * @param currentPage 当前页码
     * @param pageSize    每页大小
     * @return PageInfo<User> 包含歌手信息的分页对象
     */
    @Override
    public PageInfo<User> searchSinger(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        // 获取歌手信息
        return new PageInfo<>(searchMapper.selectSingerByKeyword(keyword));

    }

    /**
     * 根据关键字搜索歌单
     *
     * @param keyword     搜索关键字
     * @param currentPage 当前页码
     * @param pageSize    每页大小
     * @return PageInfo<MusicListDto> 包含歌单信息的分页对象
     */
    @Override
    public PageInfo<MusicListDto> searchMusicList(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<MusicListDto> list = searchMapper.selectMusicListByKeyword(keyword);
        log.info("搜索歌单： {}" + list.toString());
//        if (list.size() == 0 || list.get(0).getId() == null) return null;
        // 获取歌单信息
        return new PageInfo<>(list);

    }

    /**
     * 根据关键字搜索用户
     *
     * @param keyword     搜索关键字
     * @param currentPage 当前页码
     * @param pageSize    每页大小
     * @return PageInfo<UserDto>
     */
    @Override
    public PageInfo<UserDto> searchUser(String keyword, Integer currentPage, Integer pageSize, String userId) {
        PageHelper.startPage(currentPage, pageSize);
        List<UserDto> list = searchMapper.selectUserByKeyword(keyword);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setFollowed(followMapper.checkFollow(userId, list.get(i).getId()) > 0);
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<MusicListDto> searchCD(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return new PageInfo<>(searchMapper.selectCDByKeyword(keyword));
    }
}
